const {Product} = require("../models/productModel.js");

const time = require("moment");

//引入图片文件上传相关处理插件
const { moveFiles, removeFiles } = require("../utils/upload.js");


async function showproducts(req, res, next) {
    const { type='productName', value='', pageSize=10, currentPage=1 } = req.query;
    //总条数,获取学生模型总数据条数
    const totalNum = await Product.countDocuments();
    //总页数
    const totalPages = Math.ceil(totalNum / pageSize);
    //模糊查询
    let data = await Product
    .find({ [type]: { $regex: value, $options: "$i" } })
    .limit(pageSize - 0)//设置请求的条数
    .skip((currentPage - 1) * pageSize)// 设置跳过的条数
    .populate([{path: "categoryId"},{path:"imagesproduct" }])
    console.log(data,"获取保险产品数据");
    if(data){
        res.send({
            status: 1,
            data:  { pageSize, totalNum, totalPages, currentPage, data, },
        })
    }else{
        res.send({
            status: 0,
            message:"产品数据获取失败哦~"
        })
    }

};
//新增保险产品
async function addproducts(req, res, next) {
    console.log(req.body,"获取页面新增的保险产品数据");
    const {productName,categoryId,price,detail,state,limittime,imageId,imgname} = req.body;
    const isrename = await Product.find({productName:productName});
    console.log(isrename,666666);
    if(isrename==false){
        await Product.create({productName:productName,categoryId:categoryId,price:price,detail:detail,state:state,limittime:limittime,imageId:imageId,imgname:imgname});
        if(imgname){
            moveFiles({
                fromPath: './public/temp/',
                toPath: "./public/images/",
                filename: imgname,
            });
            removeFiles('./public/temp/')
        }
        const data = await Product.find();
        res.send({
            data: data,
            status: 1,
        });

        res.send({
            status: 0,
            message:"新增保险产品数据输入无效",
        })
    }else{
        res.send({
        status: 0,
        message:"新增保险产品名称重复",
        })
    }
}
//删除产品信息
async function deleteproducts(req, res, next) {
    const { _id} =req.body;
    const id = await Product.find({_id:_id});
    if(id){
        let data1 = await Product.deleteOne({_id});
        if(data1.deletedCount==1){
            let data = await Product.find();
            res.send({
                message: "该保险产品删除成功",
                status: 1,
                data: data,
            })
        }
    }else {
        res.send({
            message: "id获取失败",
            status: 0,
        })
    } 
}
 //修改保险信息---获取要修改的数据的id
 async function getproducts(req, res, next) {
    const stuid = await Product.find({ _id: req.params._id });
    if (stuid) {
        res.send({
            message: "保险产品数据id获取成功",
            status: 1,
            data: stuid,
        })
    } else {
        res.send({
            status: 0,
            message: "保险产品数据id获取失败",
        })
    }
    
}
//修改保险信息---点击确认修改的数据
async function updateproducts(req, res, next) {
    try {
        const { _id, productName,categoryId,price,detail,state,limittime,imageId,imgname} = req.body;
        const stuid = await Product.find({ _id: _id });
        const thestu = await Product.updateOne({ _id: _id }, { productName: productName,categoryId:categoryId,price:price,detail:detail,state:state,limittime:limittime,imageId:imageId,imgname:imgname });
        if (thestu.nModified == 1) {
            if(imgname){
                moveFiles({
                    fromPath: './public/temp/',
                    toPath: "./public/images/",
                    filename: imgname,
                });
                removeFiles('./public/temp/')
            }
                let data = await Product.find();

                res.send({
                    message: "修改成功",
                    status: 1,
                    data: data,
                })
            }else {
                res.send({
                    status: 0,
                    message: "修改失败",
                })
            }
        
    } catch (error) {
        console.log('修改失败', error);
        res.send({
            status: 0,
            message: '修改失败'
        })
    }
}
module.exports = { showproducts, getproducts,addproducts,updateproducts,deleteproducts }